package com.herohead.pushco;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.URL;

import javax.net.ssl.HttpsURLConnection;

import com.herohead.Misc;
import com.herohead.redis.GetRedisServer;
import com.herohead.redis.SendLog;

public class PushCo {

	String api_key;
	String api_secret;
	String api_message;
	Boolean useProxy = false;
	String proxyAddress = "";
	String proxyPort = "";

	private final static String USER_AGENT = "Mozilla/5.0";


	public PushCo(){}

	/**
	 * @param key - rediskey for pushco
	 * @param message - message to send to key
	 */
	public PushCo(String key, String message) {
		this(key,message,false);
	}

	/**
	 * @param key - rediskey for pushco
	 * @param message - message to send to key
	 * @param log - if you want log set to true
	 */
	public PushCo(String key, String message,boolean log) {

		checkForProxy();
		KeyRing kr = new KeyRing(key);
		String apiKey = (kr.getApiKey(key));
		String apiSecret = (kr.getSecretKey(key));
		setKey(apiKey);
		setSecret(apiSecret);
		try {
			sendMessage(message);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		if (log) {
			GetRedisServer getredis = new GetRedisServer();
			String redisHost = getredis.getRedisHost();
			SendLog l = new SendLog();
			l.setRedisHost(redisHost);
			l.setLogandWrite( "t:log:message", "message sent", "notice", "pushco", "pushco", "pushCo log", "Message sent to pushCo key = " + key + "message: " + message);
		}
	}

	/**
	 * checkForProxy - if local IP is 192.168.50.* then no proxy otherwise set to proxy.am.mt.mtnet:8080
	 */
	public void checkForProxy() {

		String s = Misc.getLocalHostIPaddress();
		if (!s.contains("192.168.50")) {
			setProxy("proxy.am.mt.mtnet", "8080");
		}
	}

	/**
	 * @return - proxy Address
	 */
	public String getProxyAddress() {
		return proxyAddress;
	}

	/**
	 * @return - proxy port
	 */
	public String getProxyPort() {
		return proxyPort;
	}

	/**
	 * @param message - message to send
	 * @return - output of sending message
	 * @throws Exception
	 */
	public String sendMessage (String message) throws Exception {

		api_message = message;

		if (api_key == null) return "Key not set";
		if (api_secret == null) return "Secret not set";

		String url = "https://api.push.co/1.0/push";
		URL obj = new URL(url);
		HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();

		//add reuqest header
		con.setRequestMethod("POST");
		con.setRequestProperty("User-Agent", USER_AGENT);
		con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
		String urlParameters = "message=" + api_message + "&api_key=" + api_key + "&api_secret=" + api_secret;

		// Send post request
		con.setDoOutput(true);
		DataOutputStream wr = new DataOutputStream(con.getOutputStream());
		wr.writeBytes(urlParameters);
		wr.flush();
		wr.close();

		int responseCode = con.getResponseCode();
		System.out.println("\nSending 'POST' request to URL : " + url);
		System.out.println("Post parameters : " + urlParameters);
		System.out.println("Response Code : " + responseCode);

		BufferedReader in = new BufferedReader(
				new InputStreamReader(con.getInputStream()));
		String inputLine;
		StringBuffer response = new StringBuffer();

		while ((inputLine = in.readLine()) != null) {
			response.append(inputLine);
		}
		in.close();

		//print result
		return response.toString();

	}

	/**
	 * @param s_api_key - set the pushCo api key
	 */
	public void setKey (String s_api_key) {
		api_key = s_api_key;
	}

	/**
	 * @param proxy - set proxy server
	 * @param port - set proxy port
	 */
	public void setProxy(String proxy, String port) {

		proxyAddress = proxy;
		proxyPort = port;

		System.setProperty("https.proxyHost", proxyAddress);
		System.setProperty("https.proxyPort", proxyPort);
		useproxy(true);
	}

	/**
	 * @param s_api_secret - secret key for the API key
	 */
	public void setSecret (String s_api_secret) {
		api_secret = s_api_secret;
	}


	/**
	 * @param useproxy - set proxy
	 */
	public void useproxy(Boolean useproxy) {
		useProxy = useproxy;
	}

}
